package com.example.demo.security.service.Impl;

import com.example.demo.common.MyBatisUtils;
import com.example.demo.security.dao.AdminTokenDao;
import com.example.demo.security.model.AdminToken;
import com.example.demo.security.service.AdminTokenService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class AdminTokenServiceImpl implements AdminTokenService {
    @Override
    public boolean adminToken(String Token, int UserId) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminTokenDao adminTokenDao =  sqlSession.getMapper(AdminTokenDao.class);
        boolean adminUser = adminTokenDao.selectByToken(Token,UserId);
        sqlSession.close();
        return adminUser;
    }


    @Override
    public int addAdminToken(String Token, int id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        AdminTokenDao adminTokenDao =  sqlSession.getMapper(AdminTokenDao.class);
        Date date=new Date();//获取一个java.util包下的Date对象
        Timestamp time=new Timestamp(date.getTime());//然后将时间转换成数据库类型的datetime类型
        Map<String ,Object> map = new HashMap<String, Object>();
        map.put("UserId",id);
        map.put("Token",Token);
        map.put("createtime",time);
        System.out.println(map);

        int userToken = adminTokenDao.addAdminToken(map);
        System.out.println(userToken);

        if (userToken>0){
            System.out.println("插入成功");
        }
        sqlSession.commit();
        sqlSession.close();

        return userToken;
    }

}
